<template>
  <div class="container">
    <el-card shadow="never">
      <el-row>
        <el-col :span="18">
          <el-form :inline="true" ref="form" :model="query" label-width="80px">
            <el-form-item label="学科名称">
              <el-input
                style="width: 190px"
                v-model="query.subjectName"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button size="mini" @click="clear">清除</el-button>
              <el-button size="mini" type="primary" @click="serch"
                >搜索</el-button
              >
            </el-form-item>
          </el-form></el-col
        >
        <el-col :span="6" style="text-align: right">
          <el-button
            icon="el-icon-edit"
            type="success"
            size="small"
            @click="addsb"
            >新增学科</el-button
          ></el-col
        >
      </el-row>

      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
      >
      </el-alert>
      <el-table class="qqq" :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="48">
        </el-table-column>
        <el-table-column prop="subjectName" label="学科名称" width="133">
        </el-table-column>
        <el-table-column prop="username" label="创建者" width="132">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="160">
          <template slot-scope="scope">
            {{ scope.row.addDate | time }}
          </template>
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示" width="132">
          <template slot-scope="scope">
            {{ scope.row.isFrontDisplay ? "是" : "否" }}
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录" width="132">
        </el-table-column>
        <el-table-column prop="tags" label="标签" width="132">
        </el-table-column>
        <el-table-column prop="totals" label="题目数量" width="132">
        </el-table-column>
        <el-table-column prop="1" label="操   作" width="240">
          <template slot-scope="scope">
            <el-button type="text" @click="camelCase(scope.row)">
              学科分类
            </el-button>
            <el-button @click="biaoqian(scope.row)" type="text">
              学科标签
            </el-button>
            <el-button @click="edit(scope.row)" type="text"> 修改 </el-button>
            <el-button @click="del(scope.row.id)" type="text"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="query.page"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="query.pagesize"
        layout="prev, pager, next, sizes, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- 弹框 -->
      <el-dialog title="新增学科" :visible.sync="dialogVisible" width="26%">
        <el-form ref="form" :model="ruleForm" label-width="80px" :rules="rules">
          <el-form-item label="学科名称" prop="subjectName">
            <el-input
              placeholder="请输入学科名称"
              v-model="ruleForm.subjectName"
              class="input"
              style="height: 20px"
            ></el-input>
          </el-form-item>
          <el-form-item label="是否显示" prop="isFrontDisplay">
            <el-switch v-model="ruleForm.isFrontDisplay"></el-switch>
          </el-form-item>
        </el-form>
        <div class="q">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, add, detail, update, remove } from '@/api/hmmm/subjects'
import dayjs from 'dayjs'
export default {
  name: 'subjects',
  data () {
    return {
      isEdit: false,
      total: 0,
      tableData: [],
      dialogVisible: false,
      ruleForm: {
        subjectName: '',
        isFrontDisplay: true
      },
      //   isFrontDisplay: false,
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' }
        ]
      },

      query: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      }
    }
  },
  methods: {
    async del (id) {
      console.log(id)
      const res = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      if (res === 'cancel') return this.$message.info('取消')
      await remove({ id: id })
      this.getSubjcts()
    },
    // 添加学科按钮
    addsb () {
      this.dialogVisible = true
      this.isEdit = false
    },
    async edit (row) {
      this.dialogVisible = true
      this.isEdit = true
      const res = await detail(row)
      this.ruleForm.subjectName = res.data.subjectName
      if (res.data.isFrontDisplay) {
        this.ruleForm.isFrontDisplay = true
      } else {
        this.ruleForm.isFrontDisplay = false
      }
      this.ruleForm.id = row.id
    },
    // 学科标签
    biaoqian (row) {
      this.$router.push({
        path: '/subjects/tags',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    // 学科分类
    camelCase (row) {
      console.log(row)
      this.$router.push({
        path: '/subjects/directorys',
        query: {
          id: row.id,
          name: row.subjectName
        }
      })
    },
    // 搜索按钮
    serch () {
      this.query.page = 1
      this.getSubjcts()
    },
    // 清除按钮
    clear () {
      this.query.subjectName = ''
      this.getSubjcts()
    },
    // 弹窗确定按钮
    add () {
      this.$refs.form.validate(async (vaild) => {
        if (this.isEdit) {
          update(this.ruleForm)

          this.$message.success('修改成功')
        } else {
          await add(this.ruleForm)

          this.$message.success('添加成功')
        }

        this.getSubjcts()
        this.dialogVisible = false
      })
    },
    // 每页条数发生改变时触发的函数
    handleSizeChange (newPageSize) {
      this.query.pagesize = newPageSize
      this.getSubjcts()
    },
    // 每天数据页数发生改变时触发的函数
    handleCurrentChange (newPage) {
      this.query.page = newPage
      this.getSubjcts()
    },
    //   获取数据列表
    async getSubjcts () {
      const res = await list(this.query)
      //   console.log(res)
      this.tableData = res.data.items
      this.total = res.data.counts
    }
  },
  //   过滤器
  filters: {
    //   时间格式过滤
    time (date) {
      return dayjs(date).format('YYYY-MM-DD hh:mm:ss')
    }
  },
  //   初始化
  created () {
    this.getSubjcts()
  }
}
</script>

<style scoped>
.qqq {
  margin-top: 15px;
}
.el-card__body {
  padding: 20px;
}
::v-deep .el-input__inner {
  height: 30px;
}
.q {
  text-align: right;
}
</style>
